#include "projectview.h"
#include "ui_projectview.h"
#include <QInputDialog>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QSettings>

#ifdef WIN32
# define PATH_SLASH '\\'
#else
# define PATH_SLASH '/'
#endif

ProjectView::ProjectView(QWidget *parent) : QWidget(parent), ui(new Ui::ProjectView)
{
    ui->setupUi(this);

    connect(ui->addLevel, SIGNAL(clicked()), this, SLOT(AddLevel()));
    connect(ui->loadLevel, SIGNAL(clicked()), this, SLOT(LoadLevel()));
    connect(ui->saveLevel, SIGNAL(clicked()), this, SLOT(SaveLevel()));
    connect(ui->testLevel, SIGNAL(clicked()), this, SLOT(TestLevel()));

    QIcon testIcon("icons/run.png");
    QIcon saveIcon("icons/save.png");
    QIcon loadIcon("icons/load.png");

    ui->testLevel->setIcon(testIcon);
    ui->saveLevel->setIcon(saveIcon);
    ui->loadLevel->setIcon(loadIcon);
}

ProjectView::~ProjectView()
{
    delete ui;
}

void ProjectView::LoadProject(const Project& project)
{
    _project = project;
    foreach (Project::Level level, _project.Levels())
    {
        ui->listLevels->addItem(level.first);
        ui->levelSelect->addItem(level.first);
    }
    ui->interfaceEditor->SetProjectPath(_project.Path());
}

void ProjectView::AddLevel()
{
    QString name = QInputDialog::getText(this, "Créer un niveau", "Nom");

    if (name != "")
    {
        QDir dir(_project.Path() + PATH_SLASH + name);

        if (!(dir.exists()) && !dir.mkdir(dir.absolutePath()))
        {
            QMessageBox::warning(this, "Impossible", "Can't create folder " + dir.absolutePath());
            return ;
        }

        QFile file(dir.absolutePath() + "/level.yml");

        if (!(file.open(QIODevice::WriteOnly | QIODevice::Text)))
        {
            QMessageBox::warning(this, "Impossible", "Can't create file level.yml");
            return ;
        }

        file.close();

      ui->listLevels->addItem(name);
      UpdatedLevelList();
      _project.Levels() << Project::Level(name, name + "/level.yml");
      _project.Save();
    }
}

void ProjectView::UpdatedLevelList()
{
    ui->levelSelect->clear();
    for (unsigned short i = 0 ; i < ui->listLevels->count() ; ++i)
      ui->levelSelect->addItem(ui->listLevels->item(i)->text());
}

void ProjectView::LoadLevel()
{
    QString name = ui->levelSelect->currentText();

    foreach (Project::Level level, _project.Levels())
    {
        if (level.first == name)
        {
            QString path = _project.Path() + PATH_SLASH + level.second;
            QFile file(path);

            if (!(file.open(QIODevice::ReadOnly | QIODevice::Text)))
            {
                QMessageBox::warning(this, "Impossible", "Can't load file " + path);
                return ;
            }
            file.close();

            if (ui->levelEditor->Load(level.first, path))
            {
                ui->levelEditor->setEnabled(true);
                ui->saveLevel->setEnabled(true);
                ui->testLevel->setEnabled(true);
            }
        }
    }
}

void ProjectView::SaveLevel()
{
    QString path = _project.Path() + PATH_SLASH;

    ui->levelEditor->Save(path);
}

void ProjectView::TestLevel()
{
    ui->levelEditor->Test(_project.Path());
}
